using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using ThoughtWorks.CruiseControl.Core;
using ThoughtWorks.CruiseControl.Core.Util;
using ThoughtWorks.CruiseControl.Core.Tasks;
using System.Xml;
using CodePlexAPI.CCNet.Publishers;

namespace CPRPTest {
  [TestFixture]
  public class CodePlexReleasePublisherTest {
    private IIntegrationResult result = null;
    private PublishBuildCondition? _bc = null;

    public CodePlexReleasePublisherTest ( ) {
      result = CreateResult ( );
      this.tval = "Test";
    }

    private string tval;

    public string TestProperty {
      get { return tval; }
    }


    [Test]
    public void TestGetCCNetPropertyString ( ) {
      string label = CodePlexAPI.CCNet.Publishers.Util.GetCCNetPropertyString<CodePlexReleasePublisherTest> (this, result, "$(Label)" );
      StringAssert.IsMatch ( result.Label, label );

      label = CodePlexAPI.CCNet.Publishers.Util.GetCCNetPropertyString<CodePlexReleasePublisherTest> ( this, result, "$(CCNetProjectUrl)" );
      Assert.IsNotEmpty ( label );

      label = CodePlexAPI.CCNet.Publishers.Util.GetCCNetPropertyString<CodePlexReleasePublisherTest> ( this, result, "$(TestProperty)" );
      Assert.IsNotEmpty ( label );

      label = CodePlexAPI.CCNet.Publishers.Util.GetCCNetPropertyString<CodePlexReleasePublisherTest> ( this, result, "$(XXX)" );
      Assert.IsNotEmpty ( label );
    }
    [Test]
    public void TestXmlProperty ( ) {
      string xpath = "!{//CodePlexRelease[@Name=\"$(Label)\"]}";
      string val = CodePlexAPI.CCNet.Publishers.Util.GetCCNetPropertyString<CodePlexReleasePublisherTest> ( this, result, xpath );
      Console.WriteLine ( val );
      StringAssert.IsMatch ( "1234", val );
    }

    [Test]
    public void TestBuildCondition ( ) {
      bool resValue = this._bc.HasValue &&
         ( ( this._bc.Value == PublishBuildCondition.ForceBuild &&
            result.BuildCondition != ThoughtWorks.CruiseControl.Remote.BuildCondition.ForceBuild ) ||
          this._bc.Value == PublishBuildCondition.IfModificationExists &&
            result.BuildCondition != ThoughtWorks.CruiseControl.Remote.BuildCondition.IfModificationExists );

      Assert.IsTrue ( !resValue, "Build Condition Null Failed." );

      this._bc = PublishBuildCondition.ForceBuild;
      resValue = this._bc.HasValue &&
         ( ( this._bc.Value == PublishBuildCondition.ForceBuild &&
            result.BuildCondition != ThoughtWorks.CruiseControl.Remote.BuildCondition.ForceBuild ) ||
          this._bc.Value == PublishBuildCondition.IfModificationExists &&
            result.BuildCondition != ThoughtWorks.CruiseControl.Remote.BuildCondition.IfModificationExists );

      Assert.IsTrue ( resValue, "Build Condition ForceBuild Failed." );


      this._bc = PublishBuildCondition.IfModificationExists;
      resValue = this._bc.HasValue &&
          ( ( this._bc.Value == PublishBuildCondition.ForceBuild &&
            result.BuildCondition != ThoughtWorks.CruiseControl.Remote.BuildCondition.ForceBuild ) ||
          this._bc.Value == PublishBuildCondition.IfModificationExists &&
            result.BuildCondition != ThoughtWorks.CruiseControl.Remote.BuildCondition.IfModificationExists );

      Assert.IsTrue ( resValue, "Build Condition IfModificationExists Failed." );

      this._bc = PublishBuildCondition.AllBuildConditions;
      resValue = this._bc.HasValue &&
          ( ( this._bc.Value == PublishBuildCondition.ForceBuild &&
            result.BuildCondition != ThoughtWorks.CruiseControl.Remote.BuildCondition.ForceBuild ) ||
          this._bc.Value == PublishBuildCondition.IfModificationExists &&
            result.BuildCondition != ThoughtWorks.CruiseControl.Remote.BuildCondition.IfModificationExists );

      Assert.IsTrue ( !resValue, "Build Condition AllBuildConditions Failed." );

    }

    private IIntegrationResult CreateResult ( ) {
      IIntegrationResult result = new IntegrationResult ( );
      result.Label = "0.1.0.1";
      result.TaskResults.Add ( new DataTaskResult ( "<CodePlexRelease Name=\"0.1.0.1\">1234</CodePlexRelease>" ) );
      return result;
    }

  }
}
